package com.wangyb.wework.service.contact;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.wangyb.wework.bo.WwGroupchatMemberQueryBo;
import com.wangyb.wework.entity.contact.WwContact;
import com.wangyb.wework.entity.contact.WwGroupchatMember;
import com.wangyb.wework.entity.employee.WwEmployee;
import com.wangyb.wework.mapper.contact.WwGroupchatMemberMapper;
import com.wangyb.wework.service.IWwEmployeeService;
import com.wangyb.wework.utils.ali.AliyunOssUtils;
import com.wangyb.wework.vo.WwGroupchatMemberVo;
import com.wangyb.wework.vo.WwGroupchatVo;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: wangyb
 * @Date: 2021/7/8 4:55 下午
 */
@Service
public class WwGroupchatMemberServiceImpl extends ServiceImpl<WwGroupchatMemberMapper, WwGroupchatMember> implements IWwGroupchatMemberService {
	@Autowired
	private IWwEmployeeService iWwEmployeeService;
	@Autowired
	private IWwContactService iWwContactService;
	@Autowired
	private IWwGroupchatService iWwGroupchatService;

	/**
	 * 统计群成员数量
	 * @param groupchatIdSet
	 * @param status
	 * @param beginTime    进群或退群开始时间
	 * @param endTime     进群或退群结束时间
	 * @return
	 */
	@Override
	public Map<Long,Integer> memberCount(Set<Long> groupchatIdSet, Integer status, Date beginTime, Date endTime){
		if (groupchatIdSet.size()==0){
			return Collections.EMPTY_MAP;
		}
		QueryWrapper<WwGroupchatMember> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("groupchat_id groupchatId,count(1) count")
			.lambda()
			.in(WwGroupchatMember::getGroupchatId,groupchatIdSet)
			.eq(status != null,WwGroupchatMember::getStatus,status)
			.between(beginTime !=null &&endTime != null && status == 1, WwGroupchatMember::getJoinTime,beginTime,endTime)
			.between(beginTime !=null &&endTime != null && status == 2, WwGroupchatMember::getQuitTime,beginTime,endTime)
			.groupBy(WwGroupchatMember::getGroupchatId);
		List<Map<String, Object>> groupchatCnt = listMaps(queryWrapper);
		Map<Long, Integer> memberCntMap = groupchatCnt.stream().collect(Collectors.toMap(m -> Long.valueOf(m.get("groupchatId").toString()), m -> Integer.valueOf(m.get("count").toString())));
		return memberCntMap;
	}

	/**
	 * 根据groupchatId查群成员
	 * @param groupchatId
	 * @return
	 */
	@Override
	public List<WwGroupchatMember> listByGroupchatId(Long groupchatId){
		return this.lambdaQuery()
			.eq(WwGroupchatMember::getGroupchatId, groupchatId)
			.eq(WwGroupchatMember::getStatus,1) //不包含退群成员
			.list();
	}
	@Override
	public TableDataInfo<WwGroupchatMemberVo> queryPageList(WwGroupchatMemberQueryBo bo) {
		PagePlus<WwGroupchatMember, WwGroupchatMemberVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), WwGroupchatMemberVo.class);
		TableDataInfo<WwGroupchatMemberVo> resp = PageUtils.buildDataInfo(result);
		resp.getRows().forEach(vo->{
			//成员类型
			if (vo.getType() == 1){
				WwEmployee employee = iWwEmployeeService.lambdaQuery().eq(WwEmployee::getWxUserId, vo.getWxUserid()).one();
				vo.setName(employee.getName());
				vo.setThumbAvatar(AliyunOssUtils.getUrl(employee.getThumbAvatar()));
			}else if(vo.getType() == 2){
				WwContact contact = iWwContactService.lambdaQuery().eq(WwContact::getWxExternalUserid, vo.getWxUserid()).one();
				if (contact != null){
					vo.setName(contact.getName());
					vo.setThumbAvatar(AliyunOssUtils.getUrl(contact.getAvatar()));
				}
			}
			//邀请人
			if (vo.getInvitorWxUserid() != null){
				WwEmployee employee = iWwEmployeeService.lambdaQuery().eq(WwEmployee::getWxUserId, vo.getInvitorWxUserid()).one();
				vo.setInvitor(employee);
			}
			//所在其他群
			//vo.getId(),vo.getGroupchatId(),vo.getCorpId();
			Set<Long> otherGroupchatIdSet = this.lambdaQuery()
				.eq(WwGroupchatMember::getCorpId, vo.getCorpId())
				.eq(WwGroupchatMember::getWxUserid, vo.getWxUserid())
				.ne(WwGroupchatMember::getGroupchatId, vo.getGroupchatId())
				.list()
				.stream()
				.map(g -> g.getGroupchatId())
				.collect(Collectors.toSet());
			if (otherGroupchatIdSet.size()>0){
				List<WwGroupchatVo> otherGroupchatList = iWwGroupchatService.listVoByIds(otherGroupchatIdSet, WwGroupchatVo.class);
				vo.setOtherGroupchatList(otherGroupchatList);
			}
		});
		return resp;
	}

	private LambdaQueryWrapper<WwGroupchatMember> buildQueryWrapper(WwGroupchatMemberQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<WwGroupchatMember> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getGroupchatId() != null, WwGroupchatMember::getGroupchatId, bo.getGroupchatId());
		lqw.eq(bo.getCorpId() != null, WwGroupchatMember::getCorpId, bo.getCorpId());
		lqw.eq(StringUtil.isNotBlank(bo.getWxUserid()), WwGroupchatMember::getWxUserid, bo.getWxUserid());
		lqw.eq(bo.getType() != null, WwGroupchatMember::getType, bo.getType());
		lqw.eq(StringUtil.isNotBlank(bo.getUnionid()), WwGroupchatMember::getUnionid, bo.getUnionid());
		lqw.eq(bo.getJoinTime() != null, WwGroupchatMember::getJoinTime, bo.getJoinTime());
		lqw.eq(bo.getJoinScene() != null, WwGroupchatMember::getJoinScene, bo.getJoinScene());
		lqw.eq(bo.getQuitTime() != null, WwGroupchatMember::getQuitTime, bo.getQuitTime());
		lqw.eq(StringUtil.isNotBlank(bo.getInvitorWxUserid()), WwGroupchatMember::getInvitorWxUserid, bo.getInvitorWxUserid());
		lqw.eq(bo.getStatus() != null, WwGroupchatMember::getStatus, bo.getStatus());
		lqw.orderByAsc(WwGroupchatMember::getJoinTime);
		return lqw;
	}


}
